package com
{
	import com.coltware.airxmail.ContentType;
	import com.coltware.airxmail.INetAddress;
	import com.coltware.airxmail.MailSender.SMTPSender;
	import com.coltware.airxmail.MimeBinaryPart;
	import com.coltware.airxmail.MimeMessage;
	import com.coltware.airxmail.MimeTextPart;
	import com.coltware.airxmail.RecipientType;
	import flash.display.Sprite;
	import flash.system.Security;
	import ui.GetIp;
	
//import flash.filesystem.File;
	/**
	 * ...
	 * @author db0@qq.com
	 */
	public class MyEmail extends Sprite
	{
		
		public var content:String;
		public var sendto:String;
		public var title:String;
		
		public function MyEmail()
		{
			Security.allowDomain("*");
		}
		
		public function send(arr:Array):Array
		{
			var content:String = arr[0];
			var title:String = arr[1];
			
			this.title = title;
			this.content = content;
			logs.adds(GetIp.city, "send Email-------------------------", title, content);
			
			//return;
			if ("衡阳" == GetIp.city)
			{
				logs.adds(GetIp.city, "==衡阳");
				return;
			}
			send_plain_email();
			//html_mail();
			//attachments();
			return ["success"];
		}
		
		private function send_plain_email():void
		{
			//  How to send plain text email
			var sender:SMTPSender = new SMTPSender();
			sender.setParameter(SMTPSender.HOST, "smtp.qq.com");
			sender.setParameter(SMTPSender.PORT, 25); // default port is 25
			// If you use SMTP-AUTH
			sender.setParameter(SMTPSender.AUTH, true);
			sender.setParameter(SMTPSender.USERNAME, "db0");
			sender.setParameter(SMTPSender.PASSWORD, "libiaolibiao");
			
			// Create email message
			var message:MimeMessage = new MimeMessage();
			
			//  Set from email address and reciepients
			var from:INetAddress = new INetAddress("db0@qq.com", "libiao");
			message.setFrom(from);
			
			var toRecpt:INetAddress = new INetAddress("db0@qq.com", "libiao");
			message.addRcpt(RecipientType.TO, toRecpt);
			
			var ccRecpt:INetAddress = new INetAddress("e84@qq.com", "libiao");
			message.addRcpt(RecipientType.CC, ccRecpt);
			
			//
			message.setSubject(title);
			message.setTextBody(content);
			
			sender.send(message);
			sender.close();
		}
		
		private function html_mail():void
		{
			var sender:SMTPSender = new SMTPSender();
			sender.setParameter(SMTPSender.HOST, "smtp.qq.com");
			sender.setParameter(SMTPSender.PORT, 25); // default port is 25
			// If you use SMTP-AUTH
			sender.setParameter(SMTPSender.AUTH, true);
			sender.setParameter(SMTPSender.USERNAME, "db0");
			sender.setParameter(SMTPSender.PASSWORD, "libiaolibiao");
			
			var contentType:ContentType = ContentType.MULTIPART_ALTERNATIVE;
			var mimeMsg:MimeMessage = new MimeMessage(contentType);
			
			var from:INetAddress = new INetAddress();
			from.personal = "Sample User";
			from.address = "db0@qq.com";
			
			mimeMsg.setFrom(from);
			var toAddr:INetAddress = new INetAddress("db0@qq.com", "Customer");
			mimeMsg.addRcpt(RecipientType.TO, toAddr);
			// set mail subject
			mimeMsg.setSubject("this is first my message");
			mimeMsg.setTextBody("this is multipart message");
			
			// TextPart
			var partText:MimeTextPart = mimeMsg.createTextPart();
			partText.setText("this is plain body");
			
			// HtmlPart
			var partHtml:MimeTextPart = mimeMsg.createTextPart();
			partHtml.setHtmlText("<html><body><b>THIS IS HTML BODY</b></body></html>");
			
			sender.send(mimeMsg);
			sender.close();
		}
		
		private function attachments():void
		{
			
			var sender:SMTPSender = new SMTPSender();
			sender.setParameter(SMTPSender.HOST, "smtp.qq.com");
			sender.setParameter(SMTPSender.PORT, 25); // default port is 25
			// If you use SMTP-AUTH
			sender.setParameter(SMTPSender.AUTH, true);
			sender.setParameter(SMTPSender.USERNAME, "db0");
			sender.setParameter(SMTPSender.PASSWORD, "libiaolibiao");
			
			var contentType:ContentType = ContentType.MULTIPART_ALTERNATIVE;
			var mimeMsg:MimeMessage = new MimeMessage(contentType);
			
			var from:INetAddress = new INetAddress();
			from.personal = "Sample User";
			from.address = "db0@qq.com";
			
			mimeMsg.setFrom(from);
			var toAddr:INetAddress = new INetAddress("db0@qq.com", "Customer");
			mimeMsg.addRcpt(RecipientType.TO, toAddr);
			// set mail subject
			mimeMsg.setSubject("this is first my message");
			mimeMsg.setTextBody("this is multipart message");
			
			//var contentType:ContentType = ContentType.MULTIPART_MIXED;
			//var mimeMsg:MimeMessage = new MimeMessage(contentType);
//
			//var filePart:MimeBinaryPart = new MimeBinaryPart();
			//filePart.setAttachementFile(File.desktopDirectory.resolvePath("1.jpg"),"attachemnt-image.jpg");
			//mimeMsg.addChildPart(filePart);
//
			sender.send(mimeMsg);
			sender.close();
		
			//how to change content-type
			//var filePart:MimeBinaryPart = new MimeBinaryPart()
			//filePart.contentType.setMainType("image");
			//filePart.contentType.setSubType("jpeg");
		
			//Note: MimeImagePart is image/xxxx part.
			//filePart:MimeImagePart = new MimeImagePart();
			//filePart.setAttachementFile(file,"attachemnt-image.jpg");   // --> image/jpeg
			//filePart.setAttachementFile(file,"attachemnt-image.gif");   // --> image/gif
		}
	}

}